/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufu.sdg12014.thriftpluresdb.server;

import br.ufu.sdg12014.base.clientreply.ThriftServer;
import br.ufu.sdg12014.base.replycoordinator.ThriftReplica;
import java.net.UnknownHostException;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;

/**
 *
 * @author SergioPeres
 */
public class Server {
    public static ServerHandler             handler;
    public static ThriftServer.Processor    processorClient;
    public static ThriftReplica.Processor   processorReply;
    public int                              clientPort = 9090;
    public int                              replyPort = 9091;
    public static String                    SERVER_FILE_NAME = "dbServerData.data";
    
    public static void main(String[] args) throws UnknownHostException {
        if((args.length != 0) && (args.length != 2))
        {
            System.err.println("SINTAXE: ... <Length Values> <Servers file name>");
            return;
        } 
        if(args.length == 0)
        {
            System.out.println("[SYS] Resuming...");
            handler = ServerHandler.restore(SERVER_FILE_NAME);
        }
        try {
            if(args.length != 0)
            {
                System.out.println("Properties: \n\tdatabase file = " + SERVER_FILE_NAME + "\n\tservers file name = " + args[1] + "\n\tdata length = " + args[0] + "\n");
                handler = new ServerHandler(SERVER_FILE_NAME, Integer.parseInt(args[0]));
            }
            System.out.println("[SYS] Starting...");
            processorClient = new ThriftServer.Processor(handler);

            Runnable simpleClient = new Runnable() {
                public void run() {
                    simple(processorClient);
                }
            };
            
            processorReply = new ThriftReplica.Processor(handler);

            Runnable simpleReply = new Runnable() {
                public void run() {
                    simple(processorReply);
                }
            };

            new Thread(simpleClient).start();
            new Thread(simpleReply).start();
            if(args.length != 0)
            {
                handler.init(args[1]);
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
    }

    public static void simple(ThriftServer.Processor processor) {
        try {
            TServerTransport serverTransport = new TServerSocket(9090);
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
            
            System.out.println("[NET] This : <-- Client [ OK ]");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void simple(ThriftReplica.Processor processor) {
        try {
            TServerTransport serverTransport = new TServerSocket(9091);
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
            
            System.out.println("[NET] This : <-- Node [ OK ]");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
